#include <common.h>
#include <print.h>
#include <task.h>

// test for task ...
// 段选择符属性定义
#define SA_RPL0      0                  // RPL = 0
#define SA_RPL1      1                  // RPL = 1
#define SA_RPL2      2                  // RPL = 2
#define SA_RPL3      3                  // RPL = 3
#define SA_TIG       0                  // TI = 0, GDT
#define SA_TIL       4                  // TI = 1, LDT

// 局部段选择符定义
#define	LDT_VIDEO_INDEX         0
#define	LDT_CODE32_INDEX        1	
#define	LDT_DATA32_INDEX        2

#define	LDT_VIDEO_SELECTOR     ((LDT_VIDEO_INDEX << 3) + SA_TIL + SA_RPL3)	
#define	LDT_CODE32_SELECTOR    ((LDT_CODE32_INDEX << 3) + SA_TIL + SA_RPL3)	
#define	LDT_DATA32_SELECTOR    ((LDT_DATA32_INDEX << 3) + SA_TIL + SA_RPL3)	

TASK task = {0};
U08 task_stack[512];
void TaskAFunc(void)
{
    printk("This is the first task.\n");
    while (1);
}


S32 main(void)
{
    SetCursorPos(0, 2);                 // 设置光标位置: (0, 2)
    SetFontColor(E_FONT_WHITE);         // 设置打印字体颜色: 白色
    printk("Start kernel...\n");        // 打印字符串 "Start kernel..."
    
    task.id = 1;
    task.name = "Task A";
    task.stack_addr = task_stack;
    task.stack_size = 512;

    task.reg.cs = LDT_CODE32_SELECTOR;
    task.reg.gs = LDT_VIDEO_SELECTOR;
    task.reg.ds = LDT_DATA32_SELECTOR;
    task.reg.es = LDT_DATA32_SELECTOR;
    task.reg.fs = LDT_DATA32_SELECTOR;
    task.reg.ss = LDT_DATA32_SELECTOR;
    
    task.reg.esp = (U32)task.stack_addr + task.stack_size;
    task.reg.eip = (U32)TaskAFunc;
    task.reg.eflags = 0x3002;

    // SwitchTo(&task);
    SWITCH_TO(&task);

    while(1);
    return 0;
}